// pages/dcDetails/dcDetails.js
const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    isConfirm: false,//提交表白
    isdialog: false,//打开对话框
    total: '',//打赏总值
    yb_num: '',//元宝个数
    other: '',//两数
    dog_list: '',//列表
    dog_name: [],//狗名列表
    selDog: false,
    index: 0,//选中狗的下标
    dog_id: '',
    barWidth: '0%',//宽度
    userInfo: {},
    page: {
      p: 0,
      psize: 3
    },
    showPic: [],
    photo_num: '',//写真总数
    formlove: {},//表白数据
    reward: {
      my: {},
      first: {},
      second: {},
      third: {},
      other: []
    },//打赏榜
    is_close: 0,//下拉收起
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      dog_id: options.dog
    })
    this.init()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getUser(id) {//狗主人信息
    wx.request({
      url: base_url + '/Public/getUserInfoById',
      data: {
        'id': id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: res => {
        if (res.data.success == 1) {
         // this.getList();
          this.setData({
            userInfo: res.data.data
          })
        }
      }
    })
  },
  init() {
    wx.showLoading({
      title: '加载中',
      mask: true,
      success: () => {
        wx.request({
          url: base_url + '/Dog/detail',
          data: {
            'dog_id': this.data.dog_id,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            if (res.data.success == '1') {
              this.setData({
                dcArr: res.data.data
              })
              this.getUser(this.data.dcArr.user_id)
              wx.setNavigationBarTitle({
                title: res.data.data.dog_name
              })
              let yb = res.data.data.yuanbao_sum;
              this.setData({
                total: Number(yb),
                yb_num: parseInt(yb / 100),
                other: yb % 100,
                barWidth: yb % 100 + '%'
              });
              // 更新表白列表
              wx.hideLoading();
              //refresh == true && wx.stopPullDownRefresh();
            } else {
              wx.showToast({
                title: '加载失败',
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  },
  confirm: function () {
    if (this.data.formlove.identity == '') {
      wx.showToast({
        title: '请输入表白身份',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showModal({
      title: '提示',
      content: '确认向对方表白？',
      success: (res) => {
        if (res.confirm) {//表白请求
          // console.log('用户点击确定')
          let formlove = this.data.formlove;
          formlove.user_token = JSON.parse(wx.getStorageSync("user")).user_token;
          formlove.pro_dog_id = this.data.dog_list[this.data.index].dog_id;
          formlove.dog_id = this.data.dog_id;
          wx.showLoading({
            title: '正在发送',
            mask: true,
            success: () => {
              wx.request({
                url: base_url + '/Profess/add',
                data: formlove,
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                method: 'POST',
                success: res => {
                  if (res.data.success == 1) {
                    console.log(res.data)
                    this.setData({//表白成功提示
                      isConfirm: true
                    })
                  } else {
                    wx.showToast({
                      title: res.data.msg,
                      icon: 'none',
                      duration: 2000
                    })
                  }
                  wx.hideLoading();
                }
              })
            }
          })

        } else if (res.cancel) {
          wx.showToast({
            title: '取消表白',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })

  },
  forlove: function () {
    this.setData({
      isdialog: true,
      isConfirm: false
    })
  },
  reward() {
    // let reward = Math.random() * 9 + 1;
    wx.showLoading({
      success: () => {
        wx.request({
          url: base_url + '/Dog/reward',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'dog_id': this.data.dog_id,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: res => {
            wx.hideLoading();
            if (res.data.success == '1') {
              let number = Number(res.data.data.number);//本次打赏数量
              let total = number + Number(this.data.total);//计算打赏总值
              this.setData({
                yb_num: parseInt(total / 100),
                other: total % 100,
                barWidth: total % 100 + '%'
              })
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }

          }
        })
      }
    })
  },
  code() {
    wx.navigateTo({
      url: '/pages/code/code?id=' + this.data.dog_id,
    })
  },
  edit() {
    wx.navigateTo({
      url: '/pages/addDogCard/addDogCard?id=' + this.data.dog_id,
    })
  },
 
  close: function (e) {
    this.setData({
      isdialog: false,
      is_close: e.currentTarget.dataset.type
    })
  },
  picDetail: function () {
    wx.navigateTo({
      url: '../picDetails/picDetails?id=' + this.data.dog_id
    })
  },
 
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value,
      selDog: true,
    })

  },
  setlove(e) {
    let type = e.currentTarget.dataset.type;
    let label = 'formlove.' + type;
    this.setData({
      [label]: e.detail.value
    })
  },
  // 收入家庭
  addFamily() {
    let http = () => {
      wx.showLoading({
        success: () => {
          wx.request({
            url: base_url + '/Dog/collectOrCancel',
            data: {
              'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
              'dog_id': this.data.dog_id,
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            success: (res) => {
              if (res.data.success == 1) {
                this.setData({
                  'dcArr.is_collect': this.data.dcArr.is_collect == 0 ? 1 : 0
                })
                wx.showToast({
                  title: '操作成功',
                  icon: 'none',
                  duration: 2000
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              }
              wx.hideLoading();
            }
          })
        }
      })

    };
    if (this.data.dcArr.is_collect == 1) {
      wx.showModal({
        title: '提示',
        content: '是否移除' + this.data.dcArr.dog_name,
        success: res => {
          if (res.confirm) {
            http();
          } else if (res.cancel) {
            wx.showToast({
              title: '已取消移除',
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    } else {
      http()
    }

  }
})